package com.southwind;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Arrays;
import java.util.List;

public class GeneratorMain {
    public static void main(String[] args) {
        AutoGenerator generator = new AutoGenerator();

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        // 连接数据库
        dataSourceConfig.setUrl("jdbc:mysql://121.199.7.212:3306/takeout");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        generator.setDataSource(dataSourceConfig);

        GlobalConfig globalConfig = new GlobalConfig();
        // 文件生成位置
        globalConfig.setOutputDir(System.getProperty("user.dir")+"/order/src/main/java");
        globalConfig.setAuthor("admin");
        generator.setGlobalConfig(globalConfig);

        PackageConfig packageConfig = new PackageConfig();
        // 父包名
        packageConfig.setParent("com.southwind");
        packageConfig.setEntity("entity");
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setMapper("mapper");
        generator.setPackageInfo(packageConfig);

        StrategyConfig strategyConfig = new StrategyConfig();
        // 如果只需要某些表，在这里设置include的参数！！！
        strategyConfig.setInclude("order_detail", "order_master");                                                           // 要生成的表名
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);                           // 表名下划线转驼峰
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);                     // 字段名下划线转驼峰
        strategyConfig.setEntityLombokModel(true);                                             // Lombok
        TableFill tableFill = new TableFill("create_time", FieldFill.INSERT);         // 自动填充的字段
        TableFill tableFill1 = new TableFill("update_time", FieldFill.INSERT_UPDATE); // 自动填充的字段
        List<TableFill> tableFills = Arrays.asList(tableFill,tableFill1);
        strategyConfig.setTableFillList(tableFills);
        generator.setStrategy(strategyConfig);

        // 【运行】
        generator.execute();
    }
}
